package com.avilyne.rest.resource;

import java.util.ArrayList;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo;

import com.avilyne.database.BookManage;
import com.avilyne.database.DbBean;
import com.avilyne.database.UserManage;
import com.avilyne.rest.model.Book;
import com.avilyne.rest.model.Page;
import com.avilyne.rest.model.Result;
import com.avilyne.rest.model.Store;

@Path("/book")
public class BookResource {
	@Context
	UriInfo uriInfo;

	@Context
	Request request;

	@GET
	@Produces(MediaType.TEXT_PLAIN)
	public String testServer() {
		return "Book service is ready!";
	}

	@GET
	@Path("get")
	@Produces(MediaType.APPLICATION_JSON)
	public Book getBook() {
		MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
		MultivaluedMap<String, String> pathParams = uriInfo.getPathParameters();

		try {
			int bookId = Integer.valueOf(queryParams.getFirst("id"));
			int page = Integer.valueOf(queryParams.getFirst("page"));

			System.out.println("Client get Book: id: " + bookId);
			return getBook(bookId, page);
		} catch (Exception e) {
			return null;
		}
	}

	@GET
	@Path("store")
	@Produces(MediaType.APPLICATION_JSON)
	public Store getStore() {
		MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
		MultivaluedMap<String, String> pathParams = uriInfo.getPathParameters();

		try {
			int bookId = Integer.valueOf(queryParams.getFirst("id"));
			int page = Integer.valueOf(queryParams.getFirst("page"));
			int limit = Integer.valueOf(queryParams.getFirst("limit"));

			System.out.println("Client get Book: id: " + bookId);
			return new BookManage().getStore(bookId, page, limit);
		} catch (NumberFormatException e) {
			return null;
		}
	}

	@GET
	@Path("stores")
	@Produces(MediaType.APPLICATION_JSON)
	public ArrayList<Store> getListStore() {
		MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
		MultivaluedMap<String, String> pathParams = uriInfo.getPathParameters();
		try {
			int page = Integer.valueOf(queryParams.getFirst("page"));
			int limit = Integer.valueOf(queryParams.getFirst("limit"));

			BookManage bookDb = new BookManage();
			return bookDb.getListStore(page, limit);
		} catch (Exception e) {
			return new ArrayList<>();
		}
	}

	@GET
	@Path("books")
	@Produces(MediaType.APPLICATION_JSON)
	public ArrayList<Book> getListBook() {
		MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
		MultivaluedMap<String, String> pathParams = uriInfo.getPathParameters();
		try {
			int storeId = Integer.valueOf(queryParams.getFirst("id"));
			int page = Integer.valueOf(queryParams.getFirst("page"));

			BookManage bookDb = new BookManage();
			return bookDb.getListBook(storeId, page);
		} catch (Exception e) {
			return new ArrayList<>();
		}
	}

	@GET
	@Path("banners")
	@Produces(MediaType.APPLICATION_JSON)
	public ArrayList<Book> getListBanner() {
		MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
		MultivaluedMap<String, String> pathParams = uriInfo.getPathParameters();
		try {
			int storeId = Integer.valueOf(queryParams.getFirst("id"));
			int page = Integer.valueOf(queryParams.getFirst("page"));

			BookManage bookDb = new BookManage();
			return bookDb.getBanner(0, page);
		} catch (Exception e) {
			return new ArrayList<>();
		}
	}

	@GET
	@Path("chaps")
	@Produces(MediaType.APPLICATION_JSON)
	public ArrayList<Page> getListChap() {
		MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
		MultivaluedMap<String, String> pathParams = uriInfo.getPathParameters();
		try {
			int storeId = Integer.valueOf(queryParams.getFirst("id"));
			int page = Integer.valueOf(queryParams.getFirst("page"));

			BookManage bookDb = new BookManage();
			return bookDb.getListChap(storeId, page);
		} catch (Exception e) {
			return new ArrayList<>();
		}
	}

	@GET
	@Path("content")
	@Produces(MediaType.APPLICATION_JSON)
	public String getContent() {
		MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
		MultivaluedMap<String, String> pathParams = uriInfo.getPathParameters();
		try {
			int storeId = Integer.valueOf(queryParams.getFirst("id"));
			int page = Integer.valueOf(queryParams.getFirst("page"));

			System.out.println("getcontent: " + storeId + " - " + page);

			BookManage bookDb = new BookManage();
			return bookDb.getContent(storeId, page);
		} catch (Exception e) {
			return "";
		}
	}

	@POST
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces(MediaType.APPLICATION_JSON)
	public Result postPerson(MultivaluedMap<String, String> personParams) {
		try {
			String userId = personParams.getFirst("user_id");
			String userName = personParams.getFirst("user_name");
			String content = personParams.getFirst("content");

			System.out.println("content info: " + content);

			return new UserManage().sendFeedBack(Integer.valueOf(userId), userName, content);
		} catch (Exception e) {
			Result rs = new Result();
			rs.message = "exception: " + e.getMessage();
			rs.result = false;
			return rs;
		}

	}

	private Book getBook(int id, int page) {
		// read from db
		DbBean db = new DbBean();
		db.connectiondb();
		return db.getBookByIdAndChapter(id, page);
	}
}
